local danggou = fk.CreateSkill{
  name = "xiaobai__danggou",
  tags = {
    Skill.Compulsory
  }
}

Fk:loadTranslationTable{
  ["xiaobai__danggou"] = "党构",
  [":xiaobai__danggou"] = "锁定技，当你指定或成为【杀】的目标时，同时询问此【杀】目标的上家和下家与你："..
    "是否弃置两张牌令此【杀】额外结算一次，若没有角色因此濒死，唯一与你意见不一致的角色翻面。"
}
---@type TrigSkelSpec<AimFunc>
local spec = {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and target == player and data.card.trueName == "slash"
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local tos = {player} ---@type Player[]
    table.insertIfNeed(tos, (data.to:getLastAlive()))
    table.insertIfNeed(tos, (data.to:getNextAlive()))
    tos = table.filter(tos, function (p)
      return #p:getCardIds("he") >= 2
    end)
    local cards = room:askToJointCards(player, {
      max_num = 2,
      min_num = 2,
      will_throw = true,
      include_equip = true, 
      players = tos,
      skill_name = danggou.name,
      cancelable = true 
    })
    local throw = cards[player] and #cards[player] > 0
    local only_different = nil
    for p, cards in pairs(cards) do
      if #cards > 0 then
        room:throwCard(cards, danggou.name, p, p)
        data.use.additionalEffect = (data.use.additionalEffect or 0) + 1
      end
    end
    for _, p in ipairs(tos) do
      if throw ~= (cards[p] and #cards[p] > 0) then
        if only_different == nil then
          only_different = p
        else
          only_different = nil
        end
      end
    end
    if only_different then
      data.card.extra_data = data.card.extra_data or {}
      data.card.extra_data.xiaobai__danggou = only_different
    end
  end
}
danggou:addEffect(fk.TargetConfirming, spec)
danggou:addEffect(fk.TargetSpecifying, spec)
danggou:addEffect(fk.CardUseFinished , {
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local room = player.room
    if not player:hasSkill(danggou.name, true, true) then return end
    local use_card = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
    if not use_card then
      if data.card.extra_data and data.card.extra_data.xiaobai__danggou then
        data.card.extra_data.xiaobai__danggou = nil
      end
      return
    end
    if #room.logic:getEventsByRule(GameEvent.Dying, 1, function (e)
      local use = e:findParent(GameEvent.Damage)
      return use and use.data.card and use.data.card == data.card
    end, use_card.id) > 0 then
      if data.card.extra_data and data.card.extra_data.xiaobai__danggou then
        data.card.extra_data.xiaobai__danggou = nil
      end
      return
    end
    print(data.card)
    return data.card.extra_data and data.card.extra_data.xiaobai__danggou
  end,
  on_use = function (self, event, target, player, data)
    data.card.extra_data.xiaobai__danggou:turnOver()
    data.card.extra_data.xiaobai__danggou = nil
  end
})

return danggou